﻿using System;
using System.Collections.Generic;

namespace Wolfpack.Periscope.Core.Infrastructure
{
    public class TinySubscriptionManager : IDisposable
    {
        private readonly ITinyMessengerHub _messengerHub;
        private readonly List<TinyMessageSubscriptionToken> _tokens;

        public TinySubscriptionManager(ITinyMessengerHub messengerHub)
        {
            _messengerHub = messengerHub;
            _tokens = new List<TinyMessageSubscriptionToken>();
        }

        public TinySubscriptionManager AddHandler<T>(Action<T> handler)
            where T: class, ITinyMessage
        {
            _messengerHub.Subscribe(handler);
            return this;
        }

        public void Dispose()
        {
            foreach (var token in _tokens)
            {
                token.Dispose();
            }
        }
    }
}